using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace Gencode.Gengui.Logon
{
	class LoginSerializer
	{
		/// <summary>
		/// Serializes LoginData object
		/// Stores all the Sql-Data
		/// </summary>
		/// <param name="data">LoginData object</param>
		public void Serialize(LoginData data)
		{
			Stream stream = null;
			try
			{
				IFormatter formatter = new BinaryFormatter();
				stream = new FileStream("data.ser", FileMode.Create, FileAccess.Write, FileShare.None);
				formatter.Serialize(stream, data);
			}
			catch
			{
				// do nothing, just ignore any possible errors
			}
			finally
			{
				if (null != stream)
					stream.Close();
			}
		}

		/// <summary>
		/// Deserializes the data.ser object
		/// Contains all sql login data
		/// </summary>
		/// <returns>LoginData object containing all sql login informations</returns>
		public LoginData Deserialize()
		{
			Stream stream = null;
			LoginData data = null;
			try
			{
				IFormatter formatter = new BinaryFormatter();
				stream = new FileStream("data.ser", FileMode.Open, FileAccess.Read, FileShare.None);
				data = (LoginData)formatter.Deserialize(stream);
			}
			catch{}// do nothing, just ignore any possible errors

			finally
			{
				if (null != stream)
					stream.Close();
			}
			return data;
		}
	}
}
